package prickworlds.game;

import org.lwjgl.opengl.Display;

import prickworlds.Prickworlds;
import prickworlds.gui.ActionEvent;
import prickworlds.gui.ActionListener;
import prickworlds.gui.Button;
import prickworlds.gui.Frame;
import prickworlds.gui.Label;
import prickworlds.gui.ProgressBar;

/**
 * 
 * @author Nicolas Winkler
 * @author Christian Holzreuter
 * 
 */
public class InGameMenu extends Frame implements ActionListener {
	protected ProgressBar life;
	protected Label scoreLabel;

	protected Frame deadMenu;
	protected Frame pauseMenu;

	protected Button resumeGame;
	protected Button newGame;
	protected Button mainMenu;
	protected Button exitGame;

	/**
	 * 
	 */
	public InGameMenu() {
		life = new ProgressBar(40, 80, 200, 30);
		life.setPercentage(100.0f);
		this.add(life);

		scoreLabel = new Label("Score: ", 40, 40, 200, 30);
		this.add(scoreLabel);

		resumeGame = new Button("Weiter", Display.getWidth() / 2 - 200, 160,
				400, 50);
		resumeGame.addActionListener(this);

		newGame = new Button("Neues Spiel", Display.getWidth() / 2 - 200, 230,
				400, 50);
		newGame.addActionListener(this);

		mainMenu = new Button("Hauptmenu", Display.getWidth() / 2 - 200, 300,
				400, 50);
		mainMenu.addActionListener(this);

		exitGame = new Button("Beenden", Display.getWidth() / 2 - 200, 370,
				400, 50);
		exitGame.addActionListener(this);

		pauseMenu = new Frame();
		deadMenu = new Frame();

		this.add(pauseMenu);
		this.add(deadMenu);

		pauseMenu.add(resumeGame);
		pauseMenu.add(newGame);
		pauseMenu.add(mainMenu);
		pauseMenu.add(exitGame);

		deadMenu.add(newGame);
		deadMenu.add(mainMenu);
		deadMenu.add(exitGame);

		hideMenues();
	}

	public void showPauseMenu() {
		pauseMenu.setVisible(true);
	}

	public void showDeadMenu() {
		deadMenu.setVisible(true);
	}

	public void hideMenues() {
		pauseMenu.setVisible(false);
		deadMenu.setVisible(false);
	}

	/**
	 * 
	 */
	@Override
	public void display() {
		scoreLabel.setText("Score: "
				+ Prickworlds.getGame().getMap().getPlayer().score);
		super.display();
	}

	/**
	 * 
	 * @param r
	 */
	void updateLife(Robot r) {
		life.setPercentage(r.getLife() * 100.0f / r.getTemplate().getMaxHp());
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == exitGame) {
			Prickworlds.requestClose();
		} else if (ae.getSource() == newGame) {
			Prickworlds.resetGame();
			hideMenues();
			Prickworlds.getGame().setRunning(true);
			Prickworlds.setProgramState(Prickworlds.getGame());
		} else if (ae.getSource() == resumeGame) {
			hideMenues();
			Prickworlds.getGame().setRunning(true);
		} else if (ae.getSource() == mainMenu) {
			Prickworlds.resetGame();
			hideMenues();
			Prickworlds.setProgramState(Prickworlds.getMainMenu());
		}
	}
}
